package com.example.printer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.epson.epos2.Epos2Exception;
import com.epson.epos2.printer.Printer;
import com.example.usbprinter.USBPrinter;

public class MainActivity extends AppCompatActivity {
    Printer mPrinter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void printerTest(View view) {
        StringBuilder textData = new StringBuilder();
        textData.append("Purchased item total number\n");
        textData.append("Sign Up and Save !\n");
        textData.append("With Preferred Saving Card\n");
        mPrinter = USBPrinter.getPrinter();
        if (mPrinter != null) {
            try {
                mPrinter.clearCommandBuffer();
                mPrinter.addText(textData.toString());
                mPrinter.addCut(Printer.CUT_FEED);
                mPrinter.sendData(Printer.PARAM_DEFAULT);
            } catch (Epos2Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(this, "未连接打印机", Toast.LENGTH_SHORT).show();
        }
    }
}